package com.dubito.argbevaluator

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.animation.ArgbEvaluatorCompat


class MainActivity : AppCompatActivity() {

    private lateinit var toolBar: Toolbar
    private var pageColorOne: Int? = null
    private var pageColorTwo: Int? = null
    private var pageColorThree: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        toolBar = findViewById(R.id.tool_bar)
        toolBar.setTitle(R.string.app_name)
        setSupportActionBar(toolBar)
        initColor()
        initPage()
    }

    private fun initColor() {
        pageColorOne = ContextCompat.getColor(this,  R.color.colorPrimary)
        pageColorTwo = ContextCompat.getColor(this, R.color.colorAccent)
        pageColorThree = ContextCompat.getColor(this, R.color.colorPrimaryDark)
    }

    private fun initPage() {
        val viewPager = findViewById<ViewPager>(R.id.view_pager)
        val itemList = mutableListOf<TextView>()
        val textViewOne = TextView(this)
        textViewOne.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        textViewOne.gravity = Gravity.CENTER
        textViewOne.text = "第一页"
        textViewOne.setBackgroundColor(pageColorOne!!)
        itemList.add(textViewOne)

        val textViewTwo = TextView(this)
        textViewTwo.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        textViewTwo.gravity = Gravity.CENTER
        textViewTwo.text = "第二页"
        textViewTwo.setBackgroundColor(pageColorTwo!!)
        itemList.add(textViewTwo)

        val textViewThree = TextView(this)
        textViewThree.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        textViewThree.gravity = Gravity.CENTER
        textViewThree.text = "第三页"
        textViewThree.setBackgroundColor(pageColorThree!!)
        itemList.add(textViewThree)


        viewPager.adapter = ContentPagerAdapter(itemList)

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float,
                                                    positionOffsetPixels: Int) {
                changeTitleColor(position, positionOffset)
            }

            override fun onPageSelected(position: Int) {

            }
        })
    }

    fun changeTitleColor(position: Int, positionOffset: Float) {
        when (position) {
            0 -> {
                val evaluate = ArgbEvaluatorCompat.getInstance()
                    .evaluate(positionOffset, pageColorOne, pageColorTwo)
                setToolBarColor(evaluate)
                setStatusBarColor(evaluate)
            }
            1 -> {
                val evaluate = ArgbEvaluatorCompat.getInstance()
                    .evaluate(positionOffset, pageColorTwo, pageColorThree)
                setToolBarColor(evaluate)
                setStatusBarColor(evaluate)
            }
            2 -> {
                setToolBarColor(pageColorThree!!)
                setStatusBarColor(pageColorThree!!)

            }
        }
    }

    private fun setStatusBarColor(color: Int) {
        window.apply {
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = color
        }
    }

    private fun setToolBarColor(color: Int) {
        toolBar.setBackgroundColor(color)
    }

    class ContentPagerAdapter(private var itemList: MutableList<TextView>) : PagerAdapter() {
        override fun isViewFromObject(view: View, obj: Any) = view == obj

        override fun getCount() = itemList.size

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            container.addView(itemList[position])
            return itemList[position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
            container.removeView(itemList[position])
        }

    }
}
